﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Library
{
    public class Sky : Component
    {
        public Model skyboxMesh;
        public Vector3 position;
        public Quaternion rotation;
        public Vector3 scale;
        public TextureCube env;
        Effect shader;
        float time;


        public Sky(Model model, Effect effect, TextureCube cube, Vector3 position, Quaternion rot, Vector3 scale)
        {
            this.DrawOrder = 0;

            this.skyboxMesh = model;
            this.shader = effect;
            this.env = cube;
            this.position = position;
            this.rotation = rot;
            this.scale = scale;
        }

        public override void Draw()
        {
            Matrix World = Matrix.CreateScale(scale) *
                Matrix.CreateFromQuaternion(rotation) *
                Matrix.CreateTranslation(Engine.Services.GetService<Camera>().Position);

            shader.Parameters["World"].SetValue(World);
            shader.Parameters["View"].SetValue(Engine.Services.GetService<Camera>().View);
            shader.Parameters["Projection"].SetValue(Engine.Services.GetService<Camera>().Projection);
            shader.Parameters["surfaceTexture"].SetValue(env);

            shader.Parameters["EyePosition"].SetValue(Engine.Services.GetService<Camera>().Position);

            for (int pass = 0; pass < shader.CurrentTechnique.Passes.Count; pass++)
            {
                for (int msh = 0; msh < skyboxMesh.Meshes.Count; msh++)
                {
                    ModelMesh mesh = skyboxMesh.Meshes[msh];
                    for (int prt = 0; prt < mesh.MeshParts.Count; prt++)
                        mesh.MeshParts[prt].Effect = shader;
                    mesh.Draw();
                }
            }
            base.Draw();
        }
    }
}
